package Cloneabe深克隆;

class Money implements Cloneable{
    public double money = 19.9;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

class Person implements Cloneable{
    public int age;
    public Money m;//组合

    public Person(int age) {
        this.age = age;
        this.m=new Money();//实例化Money
    }
    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person tmp=(Person) super.clone();//克隆出一个Person对象
        tmp.m=(Money)this.m.clone();//
        return tmp;
    }
}
public class Test {
    public static void main(String[] args) throws CloneNotSupportedException{
        Person person1=new Person(12);
        Person person2=(Person) person1.clone();
        System.out.println(person1.m.money);//19.9
        System.out.println(person2.m.money);//19.9
        System.out.println("====================");
        person2.m.money=99.99;
        System.out.println(person1.m.money);//19.9
        System.out.println(person2.m.money);//99.9
    }
}

